package com.sk.weichat.ui.lock;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.PasswordInputEditText;
import com.sk.weichat.view.SelectionFrame;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static android.widget.Toast.LENGTH_SHORT;

public class SetDeviceLockActivity extends BaseActivity {
    public static void start(Context ctx) {
        Intent intent = new Intent(ctx, SetDeviceLockActivity.class);
        ctx.startActivity(intent);
    }

    public static void reset(Activity ctx, int requestCode) {
        Intent intent = new Intent(ctx, SetDeviceLockActivity.class);
        intent.putExtra("reset", true);
        ctx.startActivityForResult(intent, requestCode);
    }

    private String firstPswd = "";
    private String secondPswd = "";

    private TextView tvTips;
    private TextView tvFinish;

    private boolean isReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_device_lock);
        getSupportActionBar().hide();
        isReset = getIntent() != null && getIntent().hasExtra("reset") ? true : false;
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvFinish = findViewById(R.id.tv_finish);
        tvFinish.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //设置密码
                //ToastUtil.showToast(mContext,);
                DeviceLockHelper.setPassword(firstPswd);
                setResult(Activity.RESULT_OK);
                finish();
            }
        });
        TextView tvTitle = findViewById(R.id.tv_title_center);
        tvTips = findViewById(R.id.tv_tip);
        tvTips.setText(isReset ? getString(R.string.tip_reset_device_lock_password_input_first) : getString(R.string.tip_set_device_lock_password_input_first));
        tvTitle.setText(isReset ? getString(R.string.change_device_password) : getString(R.string.device_lock));
        PasswordInputEditText passwordInputEditText = findViewById(R.id.edt_pswd);
        passwordInputEditText.setTextWatcher(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {
                if (charSequence.length() == 6) {
                    passwordInputEditText.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (TextUtils.isEmpty(firstPswd)) {
                                //输入第一次密码
                                firstPswd = charSequence.toString();
                                tvTips.setText(isReset ? getString(R.string.tip_reset_device_lock_password_input_second) : getString(R.string.tip_set_device_lock_password_input_second));
                                passwordInputEditText.setText("");
                                tvFinish.setVisibility(View.GONE);
                            } else {
                                if (!firstPswd.equals(charSequence.toString())) {
                                    ToastUtil.showToast(mContext, getString(R.string.tip_change_device_lock_password_input_incorrect));
                                    tvTips.setText(isReset ? getString(R.string.tip_change_device_lock_password_input_incorrect) : getString(R.string.tip_set_device_lock_password_input_first));
                                    passwordInputEditText.setText("");
                                    firstPswd = "";
                                    tvFinish.setVisibility(View.GONE);
                                } else {
                                    tvFinish.setVisibility(View.VISIBLE);
                                    //两次密码输入一致
                                    secondPswd = firstPswd;
                                }
                            }
                        }
                    }, 200);
                } else {
                    tvFinish.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }
}
